class RouterModel {
  constructor(baseUrl = '') {
    this.baseUrl = baseUrl;
    this._isLeafModel = true;
    this._modelList = [];
    this._configList = [];
  }
  get(path, handle) {
    this.addRouterConfig({
      type: 'get',
      path,
      handle,
    });
  }
  post(path, handle) {
    this.addRouterConfig({
      type: 'post',
      path,
      handle,
    });
  }
  addRouterConfig({type, path, handle}) {
    if (!this._isLeafModel) {
      console.log(new Error('非叶子节点不能添加路由配置!'));
      return;
    }
    this._configList.push({
      type,
      path: this.baseUrl + path,
      handle,
    });
  }
  addChildModel(mode) {
    if (this._isLeafModel && this._configList.length === 0) {
      this._isLeafModel = false;
    } else if (this._isLeafModel) {
      console.log(new Error('叶子节点不能添加子节点!'));
      return;
    }

    this._modelList.push(mode);
  }
  install(router) {
    if (!this._isLeafModel) {
      this._modelList.forEach(model => model.install(router));
      return;
    };

    this._configList.forEach(config => {
      const { type, path, handle } = config;
      router[type](path, handle);
    });
  }
}

module.exports = RouterModel;